package com.example.myroomdemo

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.myroomdemo.BeansConverter.AreaConverter
import com.example.myroomdemo.BeansConverter.BooksConverter
import com.example.myroomdemo.beans.Users
import com.example.myroomdemo.beans.roomSql.UserDao


/**
 * Created by LFC
on 2020/10/29.
 */
@Database(entities = [Users::class], version = 4, exportSchema = true)
@TypeConverters(AreaConverter::class, BooksConverter::class)
open abstract class AppDataBase : RoomDatabase() {

    companion object {
        private var INSTANCE: AppDataBase? = null
        private val sLock = Any()
        open fun getInstance(): AppDataBase? {
            synchronized(sLock) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(
                        MyApp.mInstance!!.applicationContext,
                        AppDataBase::class.java,
                        "lfcDemo_infos"
                    ) //崩溃后重建
                        .fallbackToDestructiveMigration() //允许主线程访问数据库
                        .allowMainThreadQueries() //升级
//                        .addMigrations(MIGRATION_1_2)
//                        .addMigrations(MIGRATION_2_3)
                        .addMigrations(MIGRATION_3_4)
                        .build()
                }
                return INSTANCE
            }
        }


        /**
         * 数据库升级
         */
        val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为旧表添加新的字段
                database.execSQL("ALTER TABLE Users ADD strHobby TEXT Default '' ")
//            创建新的数据表
//            database.execSQL("CREATE TABLE IF NOT EXISTS `book` (`roomID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT, `address` TEXT)")
            }
        }
        val MIGRATION_2_3: Migration = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为旧表添加新的字段
                database.execSQL("ALTER TABLE Users ADD strWeight TEXT Default '' ")
                database.execSQL("ALTER TABLE Users ADD Height INTEGER Default 0 NOT NULL ")
//            创建新的数据表
//            database.execSQL("CREATE TABLE IF NOT EXISTS `book` (`roomID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT, `address` TEXT)")
            }
        }
        val MIGRATION_3_4: Migration = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为旧表添加新的字段
                database.execSQL("ALTER TABLE Users ADD adsInfo TEXT Default '' ")
                database.execSQL("ALTER TABLE Users ADD listBooks TEXT Default '' ")
//            创建新的数据表
//            database.execSQL("CREATE TABLE IF NOT EXISTS `book` (`roomID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT, `address` TEXT)")
            }
        }

    }


    abstract fun UserDao(): UserDao?
}